Skip to main content

multiCall.js

The multiCall.js file has one function executeTryMultiCall() that calls all RPC URLs for a chain in parallel using promises. Workers can can execute it once per chain using their own promises to get responses for all chains at once. See a simple code example in the pricesWorker.

Each call to executeTryMultiCall() can call one or more functions (declared in the parameter contractCalls) but only on a single contract for the chain passed. See the code comments at the top of the multiCall.js file to learn more about how to use multiCall.js.

* @param {*} chain - target chain to call
* @param {*} contractAddress - target contract address on the chain to call
* @param {*} contractAbi - target contract abi
* @param {*} contractCalls - encoded function calls on the contract
* @param {*} updateDbFlag - whether or not to update the RPC provider database info
* @returns new Promise (fulfilled or rejected)

Workflow

  • Execute the contractCalls for all chain RPC URLs using internal promises.

  • For each RPC URL gets its last block number and networkID and stored then a callData array

  • Places the return values for the contractCalls into the callData array.

  • If all PRC URLs fail then the return promise is tagged as promise.status === 'rejected' and the callData will contain the error message for each RPC URL.

  • If the all RPC URLS succeed then the callData array will have rows for the results of all callData and the promise tagged as promise.status === 'fulfilled'. The callData is returned with the top row having the hightest block number.

  • If some RPC URLs fail then the callData will have a mixture of callData values and RPC URL error messages and the promise tagged as promise.status === 'fulfilled'. The callData is returned with the top row having the hightest block number and the RPC URLS that errored at the bottom of the callData array.

  • If updateDbFlag is true then the RPC provider info, for the chain, will be updated in the database CHAINS table.

    "providers": {
    "rpc": {
    "https://rpc.apechain.com/http": {
    "health": true,
    "runtime": 0.706,
    "networkId": 33139,
    "blockNumber": 21227946
    },
    ...
    }
    }